package cc.customer.myCustomer.services.impl;

import cc.customer.common.domain.ResponseBo;
import cc.customer.common.service.impl.BaseService;
import cc.customer.common.utils.DateUtil;
import cc.customer.myCustomer.damain.CusResource;
import cc.customer.myCustomer.damain.MyCustomerGroup;
import cc.customer.myCustomer.dao.CusResourceMapper;
import cc.customer.myCustomer.dao.MyCustomerGroupMapper;
import cc.customer.myCustomer.services.MyCustomerGroupService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MyCustomerGroupServiceImpl extends BaseService<MyCustomerGroup> implements MyCustomerGroupService {

   private static final Logger log = LoggerFactory.getLogger(MyCustomerGroupServiceImpl.class);

   @Autowired
   private MyCustomerGroupMapper mcgMapper;
   @Autowired
   private CusResourceMapper cusResourceMapper;

   /**添加分组**/
   @Override
   public ResponseBo selectGroupName(MyCustomerGroup group) {
      try {
         Example example = new Example(MyCustomerGroup.class);
         example.createCriteria().andCondition("c_group_name=",group.getcGroupName()).andEqualTo("cGroupUserid",group.getcGroupUserid());;
         List<MyCustomerGroup> gg = this.selectByExample(example);
         if(gg.size()>0){
            return ResponseBo.error("创建失败，分组名称已经存在");
         }else{
            group.setcGroupUserid(group.getcGroupUserid());
            group.setcGroupCreateTime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
            mcgMapper.insert(group);
            return ResponseBo.ok("创建成功");
         }
      }catch (Exception e){
         log.info(e.getMessage());
         e.printStackTrace();
         return ResponseBo.error("创建失败，请稍后再试");
      }
   }

   /**初始化客户分组列表*/
   @Override
   public List<MyCustomerGroup> selectAllGroup(MyCustomerGroup group){
      try {
      Example example = new Example(MyCustomerGroup.class);
      example.createCriteria().andEqualTo("cGroupUserid",group.getcGroupUserid());
      example.setOrderByClause(" c_group_timer asc");
      return this.selectByExample(example);
      }catch (Exception e){
         e.printStackTrace();
      }
      return new ArrayList<>();
   }

   @Override
   public JSONArray selectAllGroupToArray(MyCustomerGroup group) {
      JSONArray jsonArray = new JSONArray();
      try {
         String  userid = group.getcGroupUserid();
         Example example = new Example(MyCustomerGroup.class);
         example.createCriteria().andEqualTo("cGroupUserid",userid);
         example.setOrderByClause(" c_group_timer asc");
         List<MyCustomerGroup>  list = this.selectByExample(example);
         list.forEach( lis ->{
            JSONObject json = new JSONObject();
            json.put("id","group"+lis.getcGroupUuid());
            json.put("text",lis.getcGroupName());

            JSONArray jsonAll = new JSONArray();
            Example exampleGroup = new Example(CusResource.class);
            exampleGroup.createCriteria().andEqualTo("receiver",userid).andEqualTo("groups",lis.getcGroupUuid()).andEqualTo("delstatus","0");
            List<CusResource> listAllUsee = cusResourceMapper.selectByExample(exampleGroup);
            int i= 0;
            for(CusResource allCus:listAllUsee){
               JSONObject jsonALl = new JSONObject();
               jsonALl.put("id",allCus.getUuid());
               jsonALl.put("text",allCus.getNickname());
               jsonALl.put("types",allCus.getTypes());
               jsonAll.add(jsonALl);
               i++; //如果当前分组超过了1000个  则采用搜索的模式
               if (i >=1000){
                  break;
               }
            }
            json.put("children",jsonAll);
            json.put("icon",null);
            jsonArray.add(json);
         });
      }catch (Exception e){
         e.printStackTrace();
      }
      return jsonArray;
   }

   /**排序操作*/
   @Override
   @Transactional
   public ResponseBo sortGroup(MyCustomerGroup group){
      try {
         //获取移动的数据
         String berfoeUp = group.getBeforeUp();
         if (StringUtils.isEmpty(berfoeUp)){
            return ResponseBo.error("获取移动数据失败");
         }
         String afterup = group.getAfterUp();
         if (StringUtils.isEmpty(afterup)){
            return ResponseBo.error("获取移动数据失败");
         }
         MyCustomerGroup before = this.selectByKey(berfoeUp);

         MyCustomerGroup after   = this.selectByKey(afterup);
         if (before ==null ||after ==null){
            return ResponseBo.error("获取移动数据失败");
         }
         String beforetime = before.getcGroupCreateTime();
         String aftertime = after.getcGroupCreateTime();

         before.setcGroupCreateTime(aftertime);
         after.setcGroupCreateTime(beforetime);
         int bnum = this.updateNotNull(before);
         int fnum = this.updateNotNull(after);
         if (bnum > 0&& fnum > 0){
            return ResponseBo.ok();
         }
      }catch (Exception e){
        e.printStackTrace();
        return ResponseBo.error("操作失败，请联系网站管理员");
      }
      return ResponseBo.error();
   }

   @Override
   public ResponseBo updateGroupName(MyCustomerGroup group){
      try {
         super.updateNotNull(group);
         return ResponseBo.ok();
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error();
      }
   }
   /**删除客户**/
   @Override
   @Transactional
   public ResponseBo deleteGroupName(MyCustomerGroup group, String userid) {
      try {
         Example example = new Example(CusResource.class);
         example.createCriteria().andEqualTo("groups",group.getcGroupUuid()).andEqualTo("receiver",userid).andEqualTo("delstatus","0");
         List<CusResource> list = cusResourceMapper.selectByExample(example);
         if (!list.isEmpty()){
            return ResponseBo.error("当前分组下有客户");
         }
         int i= mcgMapper.delete(group);
         if (i > 0 ){
            return ResponseBo.ok("删除客户分组成功");
         }
      }catch (Exception e){
         log.error("删除用户分组失败："+e.getMessage());
      }
      return ResponseBo.error("删除用户分组失败");
   }




}
